package com.wx.rpc.client.proxy;

import com.wx.rpc.client.core.IDiscoveryService;

import java.lang.reflect.Proxy;

/**
 * @author wx
 * @Description: 客户端动态代理对象
 * @date 2019/3/9 000915:25
 */
public class RPCClientProxy {

    /**
     * 发现服务接口
     */
    private IDiscoveryService discoveryService;
    
    /**
     * 版本号
     */
    private String version;

    public RPCClientProxy(IDiscoveryService discoveryService, String version) {
        this.discoveryService = discoveryService;
        this.version = version;
    }

    /**
     * @description: 构建代理对象
     * @author: wx
     * @date: 2019/3/9 0009
     * @param interafaceCls
     * @return: T
     */
    public <T> T clientProxy(final Class<T> interafaceCls) {
        return (T) Proxy.newProxyInstance(interafaceCls.getClassLoader(),
                new Class[]{interafaceCls},  new RemoteInvocationHandler(discoveryService, version));
    }

}
